package one.bugu.android.demon.ui.adapter.snatch;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.snatch.WalletBean;
import one.bugu.android.demon.ui.widget.CustomListAdapter;

public class MyWalletAdapter extends CustomListAdapter<WalletBean> {

    private Context mContext;

    private DecimalFormat format = new DecimalFormat("###.##");

    public MyWalletAdapter(Context context, ArrayList<WalletBean> objList) {
        super(context, objList);
        this.mContext = context;
    }

    @Override
    protected View lpgetView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_my_wallet, null);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        if (mObjList != null) {
            setListData(mHolder, position);
        }
        return convertView;
    }

    /**
     * 填充列表数据
     *
     * @param mHolder
     * @param position
     */
    @SuppressLint("SetTextI18n")
    private void setListData(ViewHolder mHolder, int position) {
        //分割线
        mHolder.mViewDivider.setVisibility(position == mObjList.size() - 1 ? View.GONE : View.VISIBLE);
        WalletBean walletBean = mObjList.get(position);
        String titleText = "";
        int wallteType = walletBean.getWallteType();
        int imageResId = -1;
        switch (wallteType) {
            case 30:
                titleText = "BGT";
                imageResId = R.mipmap.qianbao_bgt;
                break;
            case 40:
                titleText = "算力";
                imageResId = R.mipmap.nongchang_suanli;
                break;
            case 50:
                titleText = "ETH";
                imageResId = R.mipmap.qianbao_eth;
                break;
        }
        mHolder.mIvItemWalletImage.setImageResource(imageResId);
        mHolder.mTvItemWalletUnit.setVisibility(wallteType == 40 ? View.GONE : View.VISIBLE);
        mHolder.mTvItemWalletCNY.setVisibility(wallteType == 40 ? View.GONE : View.VISIBLE);
        mHolder.mTvItemWalletName.setText(titleText);
        mHolder.mTvItemWalletCount.setText(new DecimalFormat("###.#####").format(walletBean.getBalance()) + "");
        mHolder.mTvItemWalletUnit.setText("(≈¥" + walletBean.getPrice() + "/个)");
        mHolder.mTvItemWalletCNY.setText("≈¥" + format.format(walletBean.getBalance() * walletBean.getPrice()));
    }

    public static class ViewHolder {
        public View rootView;
        public ImageView mIvItemWalletImage;
        public TextView mTvItemWalletName;
        public TextView mTvItemWalletUnit;
        public TextView mTvItemWalletCount;
        public TextView mTvItemWalletCNY;
        public View mViewDivider;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.mIvItemWalletImage = (ImageView) rootView.findViewById(R.id.iv_item_wallet_image);
            this.mTvItemWalletName = (TextView) rootView.findViewById(R.id.tv_item_wallet_name);
            this.mTvItemWalletUnit = (TextView) rootView.findViewById(R.id.tv_item_wallet_unit);
            this.mTvItemWalletCount = (TextView) rootView.findViewById(R.id.tv_item_wallet_count);
            this.mTvItemWalletCNY = (TextView) rootView.findViewById(R.id.tv_item_wallet_CNY);
            this.mViewDivider = (View) rootView.findViewById(R.id.view_divider);
        }
    }
}
